/**
 * copyright (c) 2017-2023 厦门风领科技有限公司
 * https://www.fengling.cn/
 * 
 * zengbinsi
 * 2019-01-19
 */

// const gulp = require('gulp');
// const javascriptObfuscator = require('gulp-javascript-obfuscator');
const JavaScriptObfuscator = require('javascript-obfuscator');



/**
 * 混淆代码
 * @param {*} code 源代码
 * @param {*} options 混淆参数
 */
exports.obfuscator = (code, options)=>{
    options = options || {
        compact: true,
        controlFlowFlattening: false,
        controlFlowFlatteningThreshold: 0.75,
        deadCodeInjection: true,
        deadCodeInjectionThreshold: 0.4,
        debugProtection: false,
        debugProtectionInterval: false,
        disableConsoleOutput: false,
        domainLock: [],
        identifierNamesGenerator: 'hexadecimal',
        identifiersPrefix: 'cll',
        renameGlobals: true,
        reservedNames: ['^_super'],
        reservedStrings: ['^_super'],
        rotateStringArray: true,
        rotateStringArrayEnabled: true,
        seed: 0,
        selfDefending: false,
        sourceMap: false,
        sourceMapBaseUrl: '',

        sourceMapFileName: '',
        // sourceMapMode: 'Inline',
        sourceMapSeparate: false,
        stringArray: true,
        // stringArrayEncoding: 'base64',
        stringArrayEncoding: false,

        stringArrayEncodingEnabled: true,
        stringArrayThreshold: 0.5,
        stringArrayThresholdEnabled: true,
        target: 'browser',
        transformObjectKeys: false,
        unicodeEscapeSequence: false
    };
    const obfuscationResult = JavaScriptObfuscator.obfuscate(code, options);
    return obfuscationResult.getObfuscatedCode();
};


/**
 * 混淆代码
 * @param {*} srcFileName 源文件
 * @param {*} distFileName 目标文件夹
 * @param {*} callback 回调函数
 */
exports.obfuscatorGulp = (srcFileName, distPath, callback)=>{
    // callback = callback || (()=>{});
    // const gulp = require('gulp');
    // const javascriptObfuscator = require('gulp-javascript-obfuscator');

    // gulp.src(srcFileName)
    //     .pipe(javascriptObfuscator({
    //         compact: true,
    //         controlFlowFlattening: true,
    //         controlFlowFlatteningThreshold: 0.75,
    //         identifierNamesGenerator: 'hexadecimal',
    //         stringArray: true,
    //         unicodeEscapeSequence: false,
    //         rotateStringArray: true,
    //         renameGlobals: true,
    //         debugProtection: false,
    //         debugProtectionInterval: false,
    //         deadCodeInjection: true,
    //         deadCodeInjectionThreshold: 0.4,

    //         stringArrayEncoding: 'base64',
    //         stringArrayThreshold: 0.75,
    //     }))
    //     .pipe(gulp.dest(distPath))
    //     .on('end', callback);
};
